package com.clz.appdesign.net_kotlin

import com.blankj.utilcode.util.LogUtils
import com.clz.appdesign.appdependence.baseKotlin.mvp.BasePresenter
import com.clz.appdesign.appdependence.retrofit_kotlin.BaseObserver
import com.clz.appdesign.appdependence.retrofit_kotlin.NetworkScheduler
import com.clz.appdesign.appdependence.retrofit_kotlin.RetrofitClient
import com.clz.appdesign.appdependence.retrofit_kotlin.RetryWhenNetworkException
import com.clz.appdesign.appdependence.retrofit_kotlin.entitiy.UpdateInfoBean
import com.trello.rxlifecycle2.LifecycleProvider
import com.trello.rxlifecycle2.android.ActivityEvent
import com.trello.rxlifecycle2.kotlin.bindUntilEvent
import java.util.concurrent.TimeUnit

/**
 *  @author chenliangzhi
 *  @date  2018/8/30
 *  @describe
 */
class RetrofitKotlinPresenter : BasePresenter<RetrofitKotlinView>() {
    fun testRetrofitKotlin(){
        val params = hashMapOf<String, Any>()
        params["version"] = 1
        RetrofitClient.instance.createService(ApiService2::class.java).checkUpdate(params)
                .throttleFirst(1, TimeUnit.SECONDS)
                .compose(NetworkScheduler.compose())
                .retryWhen((RetryWhenNetworkException(2, 500, 500)))
                .bindUntilEvent(view as LifecycleProvider<ActivityEvent>, ActivityEvent.DESTROY)
                .subscribe(object : BaseObserver<UpdateInfoBean>() {
                    override fun requestStart() {
                        super.requestStart()
                        view?.showLoading()
                    }
                    override fun onSucceeded(t: UpdateInfoBean) {
                        LogUtils.iTag("retrofit2", t.toString())
                    }

                    override fun onHandlerFailed(code: Int, message: String) {
                        super.onHandlerFailed(code, message)
                        LogUtils.iTag("retrofit2", message)
                    }

                    override fun requestEnd() {
                        super.requestEnd()
                        view?.hideLoading()
                    }
                })
    }

}